package com.gdyx.app.web.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

/**
 * Created by Y_Coffee on 2017/8/3.
 */
public class MD5Utils {
    public static String string2MD5(String inStr) {
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            System.out.println(e.toString());
            return "";
        }
        //加盐
        inStr = inStr + ")*35lssg57-coffee";
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++) {
            byteArray[i] = ((byte) charArray[i]);
        }
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuilder hexValue = new StringBuilder();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = md5Bytes[i] & 0xFF;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }

    public static String md52(String data, String charset) {
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] datas = data.getBytes(charset);
            byte[] des = md5.digest(datas);
            StringBuilder hexValue = new StringBuilder();
            for (int i = 0; i < des.length; i++) {
                int val = des[i] & 0xFF;
                if (val < 16) {
                    hexValue.append("0");
                }
                hexValue.append(Integer.toHexString(val));
            }
            return hexValue.toString();
        } catch (NoSuchAlgorithmException e) {
            System.out.println(e.toString());
            return "";
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";

    }

    public static void main(String[] args) {
        for (int i = 0; i < 2; i++) {
            System.out.println(UUID.randomUUID().toString().toUpperCase().trim());
//        System.out.println(UUID.randomUUID().toString().toUpperCase().trim().replaceAll("-",""));
        }
    }
}
